package com.ruoyi.business.aspect;

import com.ruoyi.business.domain.User;
import com.ruoyi.business.mapper.UserMapper;
import com.ruoyi.business.utils.ConstPools;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.impl.SysUserServiceImpl;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author bwzfy
 * @create 2023/3/5
 **/
@SuppressWarnings("ALL")
@Aspect
@Service
public class UserAspects {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private SysUserServiceImpl iSysUserService;

    public void addUserInfo(JoinPoint joinPoint) {
        if (joinPoint.getArgs()[0] instanceof SysUser) {
            SysUser sysUser = (SysUser) joinPoint.getArgs()[0];
            User user = new User();
            user.setSysUserId(sysUser.getUserId());
            user.setName(sysUser.getNickName());
            user.setId(Long.toString(sysUser.getUserId()));
            userMapper.insertUser(user);
        }
    }

    @After(value = "execution(public int com.ruoyi.system.service.impl.SysUserServiceImpl.insertUser(..))")
    public void aspectAdd(JoinPoint joinPoint) {
        addUserInfo(joinPoint);
    }

    @After(value = "execution(public boolean com.ruoyi.system.service.impl.SysUserServiceImpl.registerUser(..))")
    public void aspectRegistry(JoinPoint joinPoint) {
        addUserInfo(joinPoint);
        // 插入用户角色
        SysUser sysUser = (SysUser) joinPoint.getArgs()[0];
        sysUser.setRoleIds(new Long[]{ConstPools.COMMON_ROLE_ID});
        iSysUserService.insertUserRole(sysUser);
    }

}
